#ifndef _BBH_LINKITEM_H__
#define _BBH_LINKITEM_H__

#ifdef __cplusplus
extern "C"
{
#endif

typedef struct bbh_linkitem_s
{
    struct bbh_linkitem_s*prev;
    struct bbh_linkitem_s*next;
} bbh_linkitem_t;

#define bbh_linkitem_front(h) ((h)->next)
#define bbh_linkitem_back(h) ((h)->prev)

void bbh_linkitem_init(bbh_linkitem_t*head);
int  bbh_linkitem_isempty(bbh_linkitem_t*head);

void bbh_linkitem_pushfront(bbh_linkitem_t*head, bbh_linkitem_t*item);
void bbh_linkitem_pushback(bbh_linkitem_t*head, bbh_linkitem_t*item);
void bbh_linkitem_remove(bbh_linkitem_t*item);
int  bbh_linkitem_count(bbh_linkitem_t*head);
bbh_linkitem_t* bbh_linkitem_popfront(bbh_linkitem_t*head);
bbh_linkitem_t* bbh_linkitem_popback(bbh_linkitem_t*head);
void bbh_linkitem_clear(bbh_linkitem_t*head);
void bbh_linkitem_foreach(bbh_linkitem_t*head, void (*) (bbh_linkitem_t*, void*), void*);

// void bbh_linkitem_copy(bbh_linkitem_t*from, bbh_linkitem_t*to);
void bbh_linkitem_move(bbh_linkitem_t*from, bbh_linkitem_t*to);


#define BBH_LINKITEM_FOR_BEGIN(head,item) \
    do { \
        /* bbh_linkitem_t* */item = (head)->next; \
        bbh_linkitem_t*next = item->next; \
        for (; item != (head); item = next, next = next->next) {

#define BBH_LINKITEM_FOR_END } \
    } while(0);

#ifdef __cplusplus
}
#endif
#endif